import java.util.ArrayList;
import java.util.HashMap;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.debug.SkeletonDebugger;
 
/** Sample 7 - how to load an OgreXML model and play an animation,
 * using channels, a controller, and an AnimEventListener. */
public class HelloAnimation extends SimpleApplication
  implements AnimEventListener {
  private HashMap<String, AnimChannel> channels = new HashMap<String, AnimChannel>();
  private HashMap<String, AnimControl> controls = new HashMap<String, AnimControl>();

  private ArrayList<Node> players = new ArrayList<Node>();
  
  public static void main(String[] args) {
    HelloAnimation app = new HelloAnimation();
    app.start();
  }
 
  @Override
  public void simpleInitApp() {
    viewPort.setBackgroundColor(ColorRGBA.LightGray);

    initKeys();
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
    rootNode.addLight(dl);
    
    Node player = (Node) assetManager.loadModel("jadex3d/models/characters/jmonkey/sinbad/sinbad.j3o");
    player.setName("Player01");
    player.setLocalScale(0.5f);
    player.setLocalTranslation(-1.5f, 0, 0);
    Node player02 = (Node)player.clone();
    player02.setName("Player02");
    player02.setLocalTranslation(3, 0, 0);
    players.add(player);
    players.add(player02);
    
    
    
//    AnimControl control = player.getChild(0).getControl(AnimControl.class);
    
    
//    for (String anim : control01.getAnimationNames()) { System.out.println(anim); }
//
//    control01.addListener(this);
//    channelbase = control.createChannel();
//    channeltop = control.createChannel();
//    channelbase.setAnim("IdleBase");
//    channeltop.setAnim("IdleTop");
    
    //SekeltonDebugger
//    SkeletonDebugger skeletonDebug = 
//            new SkeletonDebugger("skeleton", control.getSkeleton());
//    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//    mat2.setColor("Color", ColorRGBA.Green);
//    mat2.getAdditionalRenderState().setDepthTest(false);
//    skeletonDebug.setMaterial(mat2);
//    player.attachChild(skeletonDebug);
    
    for(Node p : players)
    {
    	AnimControl control = p.getControl(AnimControl.class);
    	control.addListener(this);
    	controls.put(p.getName(), control);
    	
    	AnimChannel channel01 = control.createChannel();
    	AnimChannel channel02 = control.createChannel();
    	channel01.setAnim("IdleTop");
    	channel02.setAnim("IdleBase");
    	
    	channels.put(p.getName()+"t", channel01);
    	channels.put(p.getName()+"b", channel02);
    	rootNode.attachChild(p);
    }
    
  }
  
  public void simpleUpdate(float tpf) {

  }
 
  public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
	if(animName.equals("Dance")&&channels.get("Player02b").getLoopMode().equals(LoopMode.DontLoop))
	{
     	channels.get("Player02b").setAnim("IdleBase", 0.5f);
    	channels.get("Player02b").setLoopMode(LoopMode.Loop);
    	channels.get("Player02t").setAnim("IdleTop", 0.5f);
    	channels.get("Player02t").setLoopMode(LoopMode.Loop);
	}
  }
 
  public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
    // unused
  }
 
  /** Custom Keybinding: Map named actions to inputs. */
  private void initKeys() {
    inputManager.addMapping("Walk", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(actionListener, "Walk");
  }
  private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Walk") && !keyPressed) {
        if (!channels.get("Player01b").getAnimationName().equals("RunBase")) {
        	channels.get("Player01b").setAnim("RunBase", 0.5f);
        	channels.get("Player01b").setLoopMode(LoopMode.Loop);
        	channels.get("Player01t").setAnim("RunTop", 0.5f);
        	channels.get("Player01t").setLoopMode(LoopMode.Loop);
        }
        else
        {
         	channels.get("Player01b").setAnim("IdleBase", 0.3f);
        	channels.get("Player01b").setLoopMode(LoopMode.Loop);
        	channels.get("Player01t").setAnim("IdleTop", 0.3f);
        	channels.get("Player01t").setLoopMode(LoopMode.Loop);
        }
        
        if (!channels.get("Player02b").getAnimationName().equals("Dance")) {
        	channels.get("Player02b").setAnim("Dance", 0.5f);
        	channels.get("Player02b").setLoopMode(LoopMode.Loop);
        }
        else
        {
        	channels.get("Player02b").setLoopMode(LoopMode.DontLoop);
        	
//         	channels.get("Player02b").setAnim("IdleBase", 0.5f);
//        	channels.get("Player02b").setLoopMode(LoopMode.Loop);
//        	channels.get("Player02t").setAnim("IdleTop", 0.5f);
//        	channels.get("Player02t").setLoopMode(LoopMode.Loop);
        }
        
        
      }
    }
  };
}